import { request, downloadBlobFile } from 'src/utils';

export interface RoomDto {
  roomId: IdType; // 房间Id
  buildingId: number; // 楼栋ID
  roomNo: string; // 房号
  roomCode: string; // 完整房号
  roomFloor: number; // 所在楼层
  roomUsage: number; // 房间用途
  roomType: number; // 户型
  roomState: number; // 房间状态
  roomDeco: number; // 装修状态
  buildArea: number; // 建筑面积
  usageArea: number; // 使用面积
  isIdle: boolean; // 是否空置房
  manager: string; // 户主
  managerTel: string; // 户主电话
  remark: string; // 备注说明
  createdBy: string; // 创建人
  createdTime: Date; // 创建时间
}

const baseUrl = '/api/community/room';

export function getBuildingSelectTree(): Promise<SelectOption[]> {
  return request.get(baseUrl + '/building-tree');
}

/**
 * 查单个
 */
export function readData(roomId: IdType): Promise<RoomDto> {
  return request.get(baseUrl + '/' + roomId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<RoomDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(room: RoomDto): Promise<RoomDto> {
  return room.roomId ? request.put(baseUrl + '/' + room.roomId, room) : request.post(baseUrl, room);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<RoomDto>;
